
package pe.com.chilis.delivery.dao.implementacion;

import java.util.ArrayList;

import pe.com.chilis.delivery.dao.interfaz.VehiculoDAO;

import pe.com.chilis.delivery.beans.InspeccionVehicular;
import pe.com.chilis.delivery.beans.Vehiculo;

import pe.com.chilis.delivery.excepcion.ExcepcionDatos;
import pe.com.chilis.delivery.log.UtilLog;

import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.template.SqlMapDaoTemplate;

public class SQLServerVehiculoDAO extends SqlMapDaoTemplate implements VehiculoDAO {

	public SQLServerVehiculoDAO(DaoManager daoManager) {
		super(daoManager);
	}


	public int registrarVehiculo(Vehiculo vehiculo) throws ExcepcionDatos {
		try {
			daoManager.startTransaction();
			getSqlMapExecutor().insert("registrarVehiculo", vehiculo);
			daoManager.commitTransaction();
		} catch (Exception excepcion) {
			UtilLog.imprimirExcepcion(excepcion);
			throw new ExcepcionDatos(excepcion.getMessage());
		}finally{
			daoManager.endTransaction();
		}

		return Integer.valueOf(vehiculo.getIdVehiculo());
	}


	public void actualizarVehiculo(Vehiculo vehiculo) throws ExcepcionDatos {
		try {
			getSqlMapExecutor().update("actualizarVehiculo", vehiculo);
		} catch (Exception excepcion) {
			UtilLog.imprimirExcepcion(excepcion);
			throw new ExcepcionDatos(excepcion.getMessage());
		}
	}


	public void eliminarVehiculo(Vehiculo vehiculo) throws ExcepcionDatos {
		try {
			getSqlMapExecutor().delete("eliminarVehiculo", vehiculo);
		} catch (Exception excepcion) {
			UtilLog.imprimirExcepcion(excepcion);
			throw new ExcepcionDatos(excepcion.getMessage());
		}
	}

	@SuppressWarnings("unchecked")

	public ArrayList<Vehiculo> obtenerVehiculos(Vehiculo vehiculo) throws ExcepcionDatos {
		ArrayList<Vehiculo> vehiculos = new ArrayList<Vehiculo>();
		
		try {
			vehiculos = (ArrayList<Vehiculo>) queryForList("obtenerVehiculos", vehiculo);
		} catch (Exception excepcion) {
			UtilLog.imprimirExcepcion(excepcion);
			throw new ExcepcionDatos(excepcion.getMessage());
		}

		return vehiculos;
	}


	@SuppressWarnings("unchecked")
	public ArrayList<Vehiculo> buscarVehiculos(String placa) throws ExcepcionDatos {
		ArrayList<Vehiculo> vehiculos = new ArrayList<Vehiculo>();
		try {
			vehiculos = (ArrayList<Vehiculo>)queryForList("buscarVehiculos", placa);
		} catch (Exception excepcion) {
			UtilLog.imprimirExcepcion(excepcion);
			throw new ExcepcionDatos(excepcion.getMessage());
		}
		return vehiculos;
	}
	
	@Override
	public Vehiculo obtenerVehiculoPorId(int idVehiculo) throws ExcepcionDatos {
		Vehiculo vehiculo = null;

		try {
			vehiculo = (Vehiculo) queryForObject("obtenerVehiculoPorId", idVehiculo);
		} catch (Exception excepcion) {
			UtilLog.imprimirExcepcion(excepcion);
			throw new ExcepcionDatos(excepcion.getMessage());
}
		return vehiculo;
	}

	@Override
	public String verificarPlacaRegistrada(String placa) throws ExcepcionDatos {
		String placaRegistrada = null;

		try {
			placaRegistrada = (String) queryForObject("verificarPlacaRegistrada", placa);
		} catch (Exception excepcion) {
			UtilLog.imprimirExcepcion(excepcion);
			throw new ExcepcionDatos(excepcion.getMessage());
		}

		return placaRegistrada;
	}

	@Override
	public boolean registrarInspeccionVehicular(
			InspeccionVehicular inspeccionVehicular) throws ExcepcionDatos {
		try {
			daoManager.startTransaction();
			getSqlMapExecutor().insert("registrarInspeccionVehicular", inspeccionVehicular);
			daoManager.commitTransaction();
			
		} catch (Exception excepcion) {
			UtilLog.imprimirExcepcion(excepcion);
			throw new ExcepcionDatos(excepcion.getMessage());
		}finally{
			daoManager.endTransaction();
		}
		
		return true;
	}
}